<?php

class Purchasing_order extends CI_Controller {
               
	function __construct()
	{
 		parent::__construct();
		$this->load->library('form_validation');
		$this->load->library('session');
		$this->load->database();
		$this->load->helper('form');
		$this->load->helper('url');
		$this->load->model('pembelian/m_master_produk');
		$this->load->model('pembelian/m_master_supplier');	
		$this->load->model('pembelian/m_po','',TRUE);		
	}	
	function index()
	{	$this->session->unset_userdata('nopo');	
		$ses_nopo = $this->session->userdata('nopo');		
		$username = $this->session->userdata('username');  
		/*echo "<script type='text/javascript'>alert('$ses_nopo');</script>";	*/	      
		if(!empty($ses_nopo))
		{
			$nopo                 = $ses_nopo;
			$ilc['nopo']          = $nopo;

		}
		else
		{
			$nopo                 = $this->m_po->NoPo();												
			$ilc['nopo']          = $nopo;
				
		}
		
		//Nge Fecth Data Produk
		$proses    = 4;
		$param	   = array("","A");
		$form_data = array();
		
		$ilc['fetch_produk']	= $this->m_master_produk->produk($proses,$param,$form_data);	


		//Nge Fecth Data Supplier					
		$proses    = 4;
		$param	   = array();
		$form_data = array();
					
		$ilc['fetch_supplier']	= $this->m_master_supplier->supplier($proses,$param,$form_data);							

		//redirect('pembelian/purchasing_order/success');   // or whatever logic needs to occur
		$ilc['title']   = "Home &raquo Pembelian &raquo Surat Pesanan (Purchasing Order)";
		$this->load->view('pembelian/v_po',$ilc);	
		
	}
	
	function Simpan_DataPo()
	{
		$this->session->set_userdata('nopo',$this->input->post('no_po'));
		$nopo    = $this->session->userdata('nopo');
		
		/*echo "<script type='text/javascript'>alert('$nopo');</script>";*/
		
		if($this->input->post('simpan'))
		{
			$proses    = 1;	
			$param	   = array();
			$form_data = array(
	//				    'no_po' => set_value('id'),
						/*Status [ 1-Pesan 2-Penerimaan 3-Lunas 4-Pending ] */
						'no_po' 		=> $this->input->post('no_po'),
						'po_date' 		=> $this->input->post('tgl_po'),
						'kd_supplier' 	=> $this->input->post('supplier_id'),
						'tgl_jt' 		=> $this->input->post('tgl_jatuh_pembayaran'),
						'status'		=> '1' 
						);	
					
//			$ilc['fetch_supplier']	= $this->m_master_supplier->supplier($proses,$param,$form_data);							
		
			$cek      = $this->m_po->CekNoPo($nopo);

			if($cek == '0'){
				//Simpan Ke Tabel Purchasing Dulu								
				$this->m_po->f_purchasing($proses,$param,$form_data);
			}
			
			/*echo "<script type='text/javascript'>alert('$prokode');</script>"*/;
			if(!empty($prokode)){
				$nama     = $this->input->post('nama');

//				$row      = $this->m_po->get_barang($prokode);
//				$satuan   = $row->satuan;
				
				//Simpan Ke Tabel Purchasing Detail
				//$this->m_po->f_purchasing_detail($noso,$prokode,$nama,$satuan,$qty,$harga,$diskon,$total,$phg);
			}			
		}
		
		//Ubah Data Detail Purchasing [Belum]
		if($this->input->post('ubah'))
		{
			$qty      = $this->input->post('qty');
			$no       = $this->input->post('no');
            $harga    = $this->input->post('harga');
            $diskon   = $this->input->post('disc');
            $total    = $this->input->post('total');
            $phg      = $this->input->post('phg');

			$this->mso->ubahdetail($no,$noso,$harga,$qty,$diskon,$total,$phg);
		}

		if($this->input->post('clear'))
		{
 			$this->session->unset_userdata('nopo');	
		}
		
	$this->session->unset_userdata('nopo');		
	//Kembali Ke Halaman
	$ilc['title']   = "Home &raquo Pembelian &raquo Surat Pesanan (Purchasing Order)";
	$this->load->view('pembelian/v_po',$ilc);				
	}	

	function detail_produk()
	{
        $opt = $this->input->get('jns_pemasok');

		echo "<script type='text/javascript'>alert('$opt');</script>"; 

        //$harga  = $this->mso->pilih_harga($phg,$prokode);
        
        echo $harga;		
	}
}
?>